package com.idc.test;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.idc.quotation.dto.CompareDto;
import com.idc.quotation.dto.DnsDto;
import com.idc.quotation.dto.DnsDto2;
import com.idc.quotation.utils.NewTxtUtil;

public class T1 {

	public static void main(String[] args) {
		Map<String, Set<String>> map = new HashMap<>();
		Map<String, Set<String>> map2 = new HashMap<>();
		AtomicInteger no = new AtomicInteger(-1);
		File file = new File(
				"C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\AI对应IP域名-润通20250312(3).xlsx");
		EasyExcel.read(file, DnsDto.class, new ReadListener<DnsDto>() {
			@Override
			public void invoke(DnsDto data, AnalysisContext context) {
				String appName = data.getAppName().toUpperCase();
				Set<String> set = map.get(appName);
				if (null == set) {
					set = new HashSet<>();
					map.put(appName, set);
				}
				set.add(data.getIp());
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext context) {
				System.out.println("finish");
			}
		}).sheet(0).doRead();
		map.forEach((k, v) -> {
			System.out.println(k + "-->" + v.size());
		});
		System.out.println("============================");
		File file2 = new File("C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\AI-0220(2).xlsx");
		StringBuilder sb = new StringBuilder();
		EasyExcel.read(file2, DnsDto2.class, new ReadListener<DnsDto2>() {
			@Override
			public void invoke(DnsDto2 data, AnalysisContext context) {
				String appName = data.getAppName();
				if (!StringUtils.isEmpty(appName)) {
					sb.setLength(0);
					sb.append(appName);
				} else {
					appName = sb.toString();
				}
				appName = appName.toUpperCase();
				String ips = data.getIp();
				if (StringUtils.isEmpty(ips)) {
					return;
				}
				ips = ips.replace("\n", ",").replace("，", ",").replace("\t", "");
				Set<String> set = map2.get(appName);
				if (null == set) {
					set = new HashSet<>();
					map2.put(appName, set);
				}
				String[] ipArr = ips.split(",");
				for (String ip : ipArr) {
					set.add(ip);
				}
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext context) {
				System.out.println("finish");
			}
		}).sheet(0).doRead();
		map2.forEach((k, v) -> {
			System.out.println(k + "-->" + v.size());
		});
		System.out.println("-------------------------------------------");
		String fileName = "compare.xlsx";
		try (ExcelWriter excelWriter = EasyExcel.write(fileName, CompareDto.class).build()) {
			map.forEach((k, v) -> {
				String appName = k;
				Set<String> set = v;
				Set<String> set2 = map2.get(appName);
				if (null != set2) {
					System.out.println(appName + "_" + set.size() + "_" + set2.size());
					List<String> list = new ArrayList<>();
					for (String s : set) {
						if (!set2.contains(s)) {
							list.add(s);
						}
					}
					NewTxtUtil.writeTxt(list, appName + "-A.txt", "utf-8", false);
					list.clear();
					for (String s : set2) {
						if (!set.contains(s)) {
							list.add(s);
						}
					}
					NewTxtUtil.writeTxt(list, appName + "-B.txt", "utf-8", false);
					list.clear();

					Set<String> all = new HashSet<>();
					all.addAll(set);
					all.addAll(set2);
					List<CompareDto> dtos = new ArrayList<>();
					for (String s : all) {
						CompareDto dto = new CompareDto();
						dto.setIp(s);
						if (set.contains(s)) {
							dto.setA("是");
						} else {
							dto.setA("否");
						}
						if (set2.contains(s)) {
							dto.setB("是");
						} else {
							dto.setB("否");
						}
						dtos.add(dto);
					}
					WriteSheet sheet = EasyExcel.writerSheet(appName).head(CompareDto.class).build();
					excelWriter.write(dtos, sheet);
				}
			});
		} finally {
			System.out.println("done");
		}
	}
}
